<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt="">
      </div>
      <div class="e_form">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm"  class="demo-ruleForm">
          <el-form-item  prop="UserName">
            <el-input type="text" v-model="ruleForm.username" autocomplete="off" prefix-icon="iconfont icon-user
.icon-user"></el-input>
          </el-form-item>
          <el-form-item  prop="Pass">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off" prefix-icon="iconfont icon-3702mima"></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单的值
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 提交表单信息
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 重置表单信息
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    // 发起登录请求
    async login () {
      const { data: res } = await this.$http.post('/login', this.ruleForm)
      console.log(res)
      if (res.meta.status !== 200) { return this.$message.error('登录失败!') } else {
        this.$message({
          message: '登陆成功!',
          type: 'success'
        })
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
  .login_box{
    width: 450px;
    height: 300px;
    background-color: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    .avatar_box{
      width: 130px;
      height: 130px;
      border-radius: 50% ;
      position: absolute;
      border: 1px #eee solid;
      box-shadow: 0 0 10px #ddd;
      left: 154px;
      top: -65px;
      padding: 10px;
      img{
        border-radius: 50% ;
        width: 100%;
        height: 100%;
        background-color: #eee;
      }
    }
  }
}
/deep/ .el-input__inner{
  width: 380px;
  padding: 0 34px;
}
.e_form{
  position: absolute;
  left: 35px;
  top: 100px;
}
.icon{
  position: absolute;
  top: 13.2px;
  left: 10px;
  z-index: 1;
}
.btn{
  display: flex;
  justify-content: flex-end;
}
</style>
